package com.GLM.Viewer.Splash;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import com.GLM.Constants.Constant;
import com.GLM.Entity.UserInfoEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.ViewsUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Viewer.R;
import com.GLM.Viewer.FirstLogin.FirstLoginActivity;
import com.GLM.Viewer.Main.MainActivity;

public class SplashActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_splash);
		final TextView tv = (TextView)findViewById(R.id.splashTextView);
		final Animation alphAnim = new AlphaAnimation(0.0f, 1.0f);
		alphAnim.setDuration(500);
		tv.setVisibility(View.VISIBLE);
		tv.setAnimation(alphAnim);
		
		new Handler().postDelayed(new Runnable() {
			public void run() {
		
				Intent intent = null;
				if (UserInfoDAO.isFirstLogin(SplashActivity.this)) {
					Log.v(SplashActivity.class.toString(), "it's first login!");
					intent = new Intent(SplashActivity.this, FirstLoginActivity.class);
				} else {
					JSONResponse response = login();
					if (response.getStatus() == Status.ERR) {
						ViewsUtil.showErrorTip(SplashActivity.this, R.string.global_error_networkerr);
						intent = new Intent(SplashActivity.this, FirstLoginActivity.class);
					} else {
						JSONObject data = response.getData();
						int status = -1;
						try {
							status = data.getInt(Constant.RESP_STATUS);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						if (status == Constant.RESP_STATUS_SUCCESS) {
							intent = new Intent(SplashActivity.this, MainActivity.class);
						} else {
							intent = new Intent(SplashActivity.this, FirstLoginActivity.class);
						}
					}
				}
				nextActivity(intent);
			}

			private void nextActivity(Intent intent) {
				if (intent == null) {
				} else {
					SplashActivity.this.startActivity(intent);
					SplashActivity.this.finish();
				}
			}

			private JSONResponse login() {
				JSONObject jsonData = new JSONObject();
				JSONResponse jsonResponse = null;
				try {
					
					UserInfoEntity userInfo = UserInfoDAO.getUserInfo(getApplicationContext());
					jsonData.put(Constant.PARAM_ACCOUNT, userInfo.getUserAccount());
					jsonData.put(Constant.PARAM_PASSWORD, userInfo.getUserPassword());
					jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_LOGIN, jsonData);
				} catch (Exception e) {
					jsonResponse.setStatus(Status.ERR);
					e.printStackTrace();
				}
				return jsonResponse;
			}

		}, Constant.SPLASH_DISPLAY_LENGTH);
	}

}